const Recorder = require('../agora/Recorder');
const { hrTime, getLogger } = require('../utils');
const logger = require('../utils').getLogger(__filename);
const redisClient = require('../redis/redisClient');
const errors = require('../agora/error');

class ControllerBase extends Recorder {
	constructor(args, events, modal) {
		super(args, modal);
		this.events = events;
	}

	createMeetingInfo(obj) {
		return Object.assign(super.createMeetingInfo(obj), { events: JSON.stringify(this.events) });
	}

	createFilename(sid, channelName, uid, type) {
		return `${this.prefix}/${sid}_${channelName}__uid_s_${uid}__uid_e_${type}.m3u8`;
	}

	static createFilename2(sid, channelName, uid, type,prefix) {
		return `${prefix}/${sid}_${channelName}__uid_s_${uid}__uid_e_${type}.m3u8`;
	}

	static createWriteData2(eventName, userId, meetingId, channelName, uid, sid, type,modal,prefix) {
		return {
			eventName,
			module: "CLFAGORARECORD",
			"timestamp": hrTime(),
			"timestampUTC": Date.now(),
			"meeting_id": meetingId,
			channelName,
			userId,
			uid,
			modal,
			response: Date.now(),
			sid: sid,
			filename: this.createFilename2(sid, channelName, uid, type,prefix)
		}
	}

	createWriteData(eventName, userId, meetingId, channelName, uid, sid, type) {
		return {
			eventName,
			module: "CLFAGORARECORD",
			"timestamp": hrTime(),
			"timestampUTC": Date.now(),
			"meeting_id": meetingId,
			channelName,
			userId,
			uid,
			modal: this.modal,
			response: Date.now(),
			sid: sid,
			filename: this.createFilename(sid, channelName, uid, type)
		}
	}


	async start() {
		try {
			let { userId, channelName, meetingId, uid, type } = this.args
			let info = await this.getMeetingInfo(meetingId, uid);
			if (info) {
				return true;
			}
			let ret = await super.start();
			let data = this.createWriteData(this.events.start, userId, meetingId, channelName, uid, ret.sid, type);

			const meetingEvent = await redisClient.writeMeetingEvent(data.meeting_id, data)

			let meetingInfo = this.createMeetingInfo({
				sliceStartTime: 0,
				modal: this.modal,
				rid: ret.uid,
				resourceId: ret.resourceId,
				sid: ret.sid,
				redisStartEventKey: meetingEvent.pop()
			});
			let r = await redisClient.setMeetingInfo(meetingId, uid, meetingInfo);
			this.logger.info('redisClient.setMeetingInfo:' + r)
			logger.info(`agora start result:${JSON.stringify(ret)}`)
			logger.info(`agora start write data:${JSON.stringify(data)}`)

			// this.getStartTime(channelName, meetingId, uid)
			// 	.then(ret => {
			// 		data.timestampUTC = ret.serverResponse.sliceStartTime;
			// 		let diff = data.timestampUTC - data.response
			// 		logger.info(`获取sliceStartTime:${ret.serverResponse.sliceStartTime},diff:${diff}`);
			// 		data.timestamp = data.timestamp + diff;
			// 		return data;
			// 	})
			// 	.catch(e => {
			// 		logger.error('获取sliceStartTime出错');
			// 		if (e instanceof Error) {
			// 			logger.error(e);
			// 			return data;
			// 		}
			// 		logger.error(`退出代码:${e}`)
			// 	})
			// 	.then(ret => {
			// 		if (ret) {
			// 			redisClient.writeMeetingEvent(ret.meeting_id, ret)
			// 		}
			// 	});
			return data;
		} catch (e) {
			return Promise.reject(e);
		}
	}

	async stop() {
		try {
			let { userId, channelName, meetingId, uid, type } = this.args
			let ret = await super.stop();
			logger.info(`agora stop :${JSON.stringify(ret)}`);
			let data = this.createWriteData(this.events.stop, userId, meetingId, channelName, uid, ret.sid, type);
			const rts = await redisClient.writeMeetingEvent(data.meeting_id, data)
			logger.info(rts)
			logger.info(data)
			return data;
		} catch (e) {
			if (e instanceof errors.RecordsNoExists) {
				return true;
			}
			return Promise.reject(e);
		}
	}

}

module.exports = ControllerBase
